/*
 * Copyright (c) 2025 李伟国
 * SQL-API is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.moyu.sql.business.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moyu.sql.business.entity.MoyuSqlParam;
import com.moyu.sql.business.entity.MoyuSqlParamGroup;
import com.moyu.sql.business.entity.vo.MoyuSqlParamGroupDto;
import com.moyu.sql.business.mapper.MoyuSqlParamMapper;
import com.moyu.sql.business.service.IMoyuSqlParamGroupService;
import com.moyu.sql.business.service.IMoyuSqlParamService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class MoyuSqlParamServiceImpl extends ServiceImpl<MoyuSqlParamMapper, MoyuSqlParam> implements IMoyuSqlParamService {

    @Resource
    private IMoyuSqlParamGroupService groupService;

    /**
     * 根据参数组id查询参数列表
     *
     * @param sqlParamGroupId 参数组id
     * @return 参数列表
     */
    @Override
    public List<MoyuSqlParam> selectBySqlParamGroupId(String sqlParamGroupId) {
        return this.lambdaQuery()
                .eq(MoyuSqlParam::getParamGroupId, sqlParamGroupId)
                .list();
    }

    /**
     * 根据参数id查询参数
     *
     * @param groupDto 参数分组列表
     * @return 参数分组id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public MoyuSqlParamGroup saveOrUpdateBatch(MoyuSqlParamGroupDto groupDto) {
        List<MoyuSqlParam> moyuSqlParamList = groupDto.getMoyuSqlParamList();
        MoyuSqlParamGroup moyuSqlParamGroup = new MoyuSqlParamGroup();
        if (moyuSqlParamList == null || moyuSqlParamList.isEmpty()) {
            return moyuSqlParamGroup;
        }
        if (StringUtils.isNotBlank(groupDto.getParamGroupId())) {
            moyuSqlParamGroup = groupService.getById(groupDto.getParamGroupId());
        } else if (StringUtils.isNotBlank(moyuSqlParamList.get(0).getParamGroupId())) {
            moyuSqlParamGroup = groupService.getById(moyuSqlParamList.get(0).getParamGroupId());
        } else {
            moyuSqlParamGroup.setParamGroupName("默认参数组" + System.currentTimeMillis());
            groupService.save(moyuSqlParamGroup);
        }

        for (MoyuSqlParam moyuSqlParam : moyuSqlParamList) {
            moyuSqlParam.setParamGroupId(moyuSqlParamGroup.getParamGroupId());
        }
        this.saveOrUpdateBatch(moyuSqlParamList);
        return moyuSqlParamGroup;
    }

    /**
     * 通过分组id查询参数列表
     *
     * @param groupId 分组id
     * @return 参数列表
     */
    @Override
    public MoyuSqlParamGroupDto getByGroupId(String groupId) {
        MoyuSqlParamGroupDto groupDto = new MoyuSqlParamGroupDto();
        if (StringUtils.isBlank(groupId)) {
            return groupDto;
        }
        MoyuSqlParamGroup groupObj = groupService.getById(groupId);
        BeanUtils.copyProperties(groupObj, groupDto);
        List<MoyuSqlParam> moyuSqlParams = this.selectBySqlParamGroupId(groupId);
        groupDto.setMoyuSqlParamList(moyuSqlParams);
        return groupDto;
    }
}
